<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
 <body>
<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};

RM.Common = {};

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
RM.BaseLargeImageURL = RM.RootURL + 'RM/userdata/images/system/large/';
RM.BaseSmallImageURL = RM.RootURL + 'RM/userdata/images/system/small/';
RM.BaseMenuImageURL  = RM.RootURL + 'RM/userdata/images/system/menu/';
RM.BaseUnitImageURL  = RM.RootURL + 'RM/userdata/images/media/';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/common.js"></script>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- ExtJS Base -->
<?php
$config = Zend_Registry::get('config');
if ((int)$config->get('js')->get('debug')->get('user') == 1) {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
<?php
} else {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
<?php
}
?>
<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/HospitalityPrices/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/plugins/Hospitality/css/personspicker.css" />

<?php
$criteria = RM_Reservation_Manager::getInstance()->getCriteria();
if (!isset($criteria)){
    ?>
    <center><div id="RM_personspicker_message"><?php echo $this->_translate->_('User.Hospitality.PersonsPicker','error');?></div></center>
    <?php
    die();
}

$config = new RM_Config;
$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);
$peopleMaximums = $priceSystem->getPeopleMaximums($this->unit_id); // returns array containing "max_occupancy"=>$max_occupancy,"max_adults"=>$max_adults,"max_children"=>$max_children,"max_infants"=>$max_infants
?>

<div class="RM_personspicker_modal_container">
    <div class="RM_personspicker_modal_header">
            <?php echo $this->_translate->_('User.Hospitality.PersonsPicker','Title');?>
    </div>
    <div class="RM_personsselector">

            <!-- maximum people selection -->
            <div class="RM_personspicker_adults">
                <div class="RM_personspicker_label"><?php echo $this->_translate->_('User.DatePicker','Adults');?></div>
                    <select id="adults">
                        <?php
                        for ($i = 1; $i <= (int)$peopleMaximums['max_adults']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->adults==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php if ((int)$peopleMaximums['max_children']>0){?>
            <div class="RM_personspicker_children">
                <div class="RM_personspicker_label"><?php echo $this->_translate->_('User.DatePicker','Children');?></div>
                    <select id="children" class="RM_hospitality_feild">
                        <?php
                        for ($i = 0; $i <= (int)$peopleMaximums['max_children']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->children==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php 
            } else {
                ?>
                <input type="hidden" id="children" value="0">
                <?php
            }
            if ((int)$peopleMaximums['max_infants']>0){?>
            <div class="RM_personspicker_infants">
                <div class="RM_personspicker_label"><?php echo $this->_translate->_('User.DatePicker','Infants');?></div>
                    <select id="infants" class="RM_hospitality_feild">
                        <?php
                        for ($i = 0; $i <= (int)$peopleMaximums['max_infants']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->infants==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php
            } else {
                ?>
                <input type="hidden" id="infants" value="0">
                <?php
            }
            ?>
            <center><div id="RM_personspicker_message" style="display:none;"></div></center>
        </div>
    <div id="RM_personspicker_button">
        <input name="" type="button" value="OK" class="button" onclick="RM_Submit()" />
    </div>
</div>

</body>
</html>

<script type="text/javascript">

function RM_Submit(){

    //check we are not exceeding our unit people limits.
    var adultsValue = parseInt(document.getElementById('adults').value);
    var childrenValue = parseInt(document.getElementById('children').value);
    var infantsValue = parseInt(document.getElementById('infants').value);
    var maxOccupancy = <?php echo $peopleMaximums['max_occupancy'];?>;

    if ((adultsValue+childrenValue+infantsValue)>maxOccupancy){
        var message = "<?php echo $this->_translate->_('User.DatePicker','ExceededMaxOccupancy');?>";
        document.getElementById('RM_personspicker_message').innerHTML = message.replace(/\[XX]/g, maxOccupancy);
        document.getElementById('RM_personspicker_message').style.display = "block";
        return;
    }

    document.getElementById('RM_personspicker_message').style.display = "block";
    document.getElementById('RM_personspicker_message').innerHTML = "<?php echo $this->_translate->_('User.Hospitality.PersonsPicker','Saving');?>";

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'HospitalityPrices',
            action: 'setdatecriteriajson'
        }),
        params: {
            adults: adultsValue,
            children: childrenValue,
            infants: infantsValue
        },
        method: 'POST',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json==="saved"){
                window.parent.location.reload();
            } else {
                document.getElementById('RM_personspicker_message').innerHTML = json.error;
            }
        },
        failure: function(){
            document.getElementById('RM_personspicker_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};
</script>